﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace LyricsMarger
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void btnTimingSelect_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofdt = new OpenFileDialog();
            ofdt.FileName = "NewTiming.txt";
            ofdt.InitialDirectory = System.Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory);
            ofdt.Filter = "テキストファイル(*.txt)|*.txt|すべてのファイル|*.*";
            ofdt.ShowHelp = true;
            if (ofdt.ShowDialog() == DialogResult.OK)
            {
                try
                {
                    System.IO.StreamReader sr = new System.IO.StreamReader(
                        ofdt.FileName, Encoding.GetEncoding("Shift_JIS"));
                    while (!sr.EndOfStream)
                    {
                        tbTimingSelect.AppendText(sr.ReadLine().ToString()+Environment.NewLine);
                    }
                    // 読み込み完了
                    lblTimingNum.Text = tbTimingSelect.Lines.Length.ToString();
                    btnTimingSelect.Enabled = false;
                    if(btnLyrics.Enabled==false){
                        btnMarge.Enabled=true;
                    }
                }
                catch (Exception ex) {
                    MessageBox.Show(ex.Message);
                }
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofdl = new OpenFileDialog();
            ofdl.FileName = "NewLyrics.txt";
            ofdl.InitialDirectory = System.Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory);
            ofdl.Filter = "テキストファイル(*.txt)|*.txt|すべてのファイル|*.*";
            ofdl.ShowHelp = true;
            if (ofdl.ShowDialog() == DialogResult.OK)
            {
                try
                {
                    System.IO.StreamReader sr = new System.IO.StreamReader(
                        ofdl.FileName, Encoding.GetEncoding("Shift_JIS"));
                    while (!sr.EndOfStream)
                    {/*
                        string ch = ((char)sr.Read()).ToString();
                        if ((ch != " ") && (ch != Environment.NewLine) && (ch != "\t") && (ch!="\n"))
                        {
                            tbLyricsSelect.AppendText(ch.ToString()+Environment.NewLine);
                        }*/
                        tbLyricsSelect.AppendText(sr.ReadLine() + Environment.NewLine);
                    }
                    // 読み込み完了
                    lblLyricsNum.Text = tbLyricsSelect.Lines.Length.ToString();
                    btnLyrics.Enabled = false;
                    if (btnTimingSelect.Enabled == false)
                    {
                        btnMarge.Enabled = true;
                    }
                }
                catch (Exception ex) { MessageBox.Show(ex.Message); }
            }
        }

        private void btnMarge_Click(object sender, EventArgs e)
        {
            if (tbTimingSelect.Lines.Length != tbLyricsSelect.Lines.Length)
            {
                MessageBox.Show(tbTimingSelect.Lines.Length.ToString() + '\t'+
                                tbLyricsSelect.Lines.Length.ToString() +
                                "\t文字数が違います！やりなおせ");
            }
            else
            {
                SaveFileDialog sfd = new SaveFileDialog();
                sfd.FileName = "NewLyrics.txt";
                sfd.InitialDirectory = System.Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory);
                sfd.Filter = "テキストファイル(*.txt)|*.txt|すべてのファイル|*.*";
                sfd.FilterIndex = 1;
                sfd.Title = "保存先のファイルを指定してください";
                sfd.RestoreDirectory = true;
                sfd.ShowHelp = true;
                if (sfd.ShowDialog() == DialogResult.OK)
                {
                    System.IO.StreamWriter sw = new System.IO.StreamWriter(
                        sfd.FileName, false, Encoding.GetEncoding("Shift_JIS"));
                    tbMarged.Text = "";
                    int length = tbTimingSelect.Lines.Length;
                    int i = 0;
                    while (i < length)
                    {
                        string str = tbTimingSelect.Lines[i].ToString() + ',' +
                                     tbLyricsSelect.Lines[i].ToString();
                        sw.WriteLine(str);
                        tbMarged.AppendText(str + Environment.NewLine);
                        i++;
                    }
                    sw.Close();
                }
            }
        }

        private void tbTimingSelect_TextChanged(object sender, EventArgs e)
        {
            lblTimingNum.Text = tbTimingSelect.Lines.Length.ToString();
        }

        private void tbLyricsSelect_TextChanged(object sender, EventArgs e)
        {
            lblLyricsNum.Text = tbLyricsSelect.Lines.Length.ToString();
        }

        private void btnTimingOutput_Click(object sender, EventArgs e)
        {
            SaveFileDialog sfd = new SaveFileDialog();
            sfd.FileName = "NewTiming.txt";
            sfd.InitialDirectory = System.Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory);
            sfd.Filter = "テキストファイル(*.txt)|*.txt|すべてのファイル|*.*";
            sfd.FilterIndex = 1;
            sfd.Title = "保存先のファイルを指定してください";
            sfd.RestoreDirectory = true;
            sfd.ShowHelp = true;
            if (sfd.ShowDialog() == DialogResult.OK)
            {
                System.IO.StreamWriter sw = new System.IO.StreamWriter(
                    sfd.FileName, false, Encoding.GetEncoding("Shift_JIS"));
                int length = tbTimingSelect.Lines.Length;
                int i = 0;
                while (i < length)
                {
                    string str = tbTimingSelect.Lines[i].ToString();
                    sw.WriteLine(str);
                    i++;
                }
                sw.Close();
            }
        }

        private void btnLyricsOutput_Click(object sender, EventArgs e)
        {
            SaveFileDialog sfd = new SaveFileDialog();
            sfd.FileName = "NewLyrics.txt";
            sfd.InitialDirectory = System.Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory);
            sfd.Filter = "テキストファイル(*.txt)|*.txt|すべてのファイル|*.*";
            sfd.FilterIndex = 1;
            sfd.Title = "保存先のファイルを指定してください";
            sfd.RestoreDirectory = true;
            sfd.ShowHelp = true;
            if (sfd.ShowDialog() == DialogResult.OK)
            {
                System.IO.StreamWriter sw = new System.IO.StreamWriter(
                    sfd.FileName, false, Encoding.GetEncoding("Shift_JIS"));
                int length = tbLyricsSelect.Lines.Length;
                int i = 0;
                while (i < length)
                {
                    string str = tbLyricsSelect.Lines[i].ToString();
                    sw.WriteLine(str);
                    i++;
                }
                sw.Close();
            }
        }

        private void btnMargeOutput_Click(object sender, EventArgs e)
        {
            SaveFileDialog sfd = new SaveFileDialog();
            sfd.FileName = "NewSubtitle.txt";
            sfd.InitialDirectory = System.Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory);
            sfd.Filter = "テキストファイル(*.txt)|*.txt|すべてのファイル|*.*";
            sfd.FilterIndex = 1;
            sfd.Title = "保存先のファイルを指定してください";
            sfd.RestoreDirectory = true;
            sfd.ShowHelp = true;
            if (sfd.ShowDialog() == DialogResult.OK)
            {
                System.IO.StreamWriter sw = new System.IO.StreamWriter(
                    sfd.FileName, false, Encoding.GetEncoding("Shift_JIS"));
                int length = tbMarged.Lines.Length;
                int i = 0;
                while (i < length)
                {
                    string str = tbMarged.Lines[i].ToString();
                    sw.WriteLine(str);
                    i++;
                }
                sw.Close();
            }
        }
    }
}
